package components.map.componentdrag
{
	import capricornus.core.StageManager;
	
	import components.map.MapView;
	import components.map.item.NPCItem;
	import components.map.item.SceneItem;
	import components.map.utils.SceneItemType;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	public class ComponentDragManager
	{
		private var _view:MapView;
		private var _container:Sprite;
		private var _itemLayer:Sprite;
		
		private var _w:int;
		private var _h:int;
		
		private var _currentIndex:int = SceneItemType.NONE;
		private var _currentDrag:SceneItem;
		
		private var _dict:Dictionary;
		
		public function ComponentDragManager( container:Sprite, view:MapView )
		{
			_view = view;
			_itemLayer = view.itemLayer;
			
			init();
		}
		
		public function clear():void
		{
			_isMapInit = false;
			var len:int = _itemLayer.numChildren;
			while( len-->0 )
			{
				_itemLayer.removeChildAt(0);
			}
			for( var i:* in _dict )
			{
				var si:SceneItem = _dict[i];
				si.destroy();
				delete _dict[i];
			}
		}
		
		private function init():void
		{
//			_container.alpha = 0.7;
			_container = new Sprite();
			_view.addChild( _container );
			_container.mouseChildren = false;
			_container.mouseEnabled = false;
			_dict = new Dictionary();
		}
		
		private var _isMapInit:Boolean = false;
		public function set isMapInit( value:Boolean ):void
		{
			_isMapInit = value;
		}
		
		public function setSize( w:int, h:int ):void
		{
			_w = w;
			_h = h;
		}
		
		public function startDrag( index:int ):void
		{
			if( !_isMapInit ) return;
			if( index==_currentIndex ) return;
			_currentIndex = index;
				
			var dis:SceneItem = null;
			if( index==SceneItemType.NONE )
			{
				dis = null; 
			}
			else if( index==SceneItemType.NPC )
			{
				dis = new NPCItem();
			}
			
			if( dis==null )
			{
				if( _currentDrag != null )
				{
					_currentDrag.destroy();
					_container.removeChild( _currentDrag );
					_currentDrag = null;
					_view.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseOver );
					_view.removeEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
				}
			}
			else
			{
				if( _currentDrag==null )
				{
					_view.addEventListener( MouseEvent.MOUSE_MOVE, onMouseOver, false, 0, true );
					_view.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true );
				}
				else
				{
					_currentDrag.destroy();
					_container.removeChild( _currentDrag );
				}
				_currentDrag = dis;
				_container.addChild( _currentDrag );
			}
		}
		
		private function onMouseOver( e:MouseEvent ):void
		{
			if( _currentDrag==null ) return;
			
			var pt:Point = new Point( e.stageX, e.stageY );
			pt = _container.globalToLocal( pt );
			_currentDrag.x = int(pt.x);
			_currentDrag.y = int(pt.y);
			
			trace( pt.x, pt.y );
				
			/*_currentDrag.x = e.stageX;
			_currentDrag.y = e.stageY;*/
		}
		
		private function onMouseDown( e:MouseEvent ):void
		{
			var x:int = _container.mouseX;
			var y:int = _container.mouseY;
			if( x < 0 || x > _w || y < 0 || y > _h )
			{
				return;
			}
			
			if( _currentDrag != null )
			{
				var si:SceneItem = _currentDrag.clone();
				si.setLocation( _currentDrag.x, _currentDrag.y );
				_itemLayer.addChild( si );
				_dict[si] = si;
				
				if( e.shiftKey )
				{
					
				}
				else
				{
					startDrag( SceneItemType.NONE );
				}
			}
		}
	}
}





















